package gameObject
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.CubeGeometry;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix3D;
	import flash.net.Responder;
	import flash.utils.Timer;
	
	import game.P2pWorld;
	
	import gameObject.resources3D.BulletResource;
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.WeaponResource;
	
	/**
	 * 战斗中玩家的低模武器
	 */	
	public class WeaponMain extends GameObjectBase
	{
		private var _pid:int;
		
		private var _dur:int;
		private var _maxDur:int;
		private var _bulletID:int;
		private var _maxBullets:int;
		private var _reload:Number;
		private var _speed:Number;
		private var _pow:int;
		
		private var _bulletRes:BulletResource;
		
		public function WeaponMain(pid:int)
		{
			Global.phpServer.call("robotServer.getMainWeapon", new Responder(gotValue),pid);
		}
		
		public function get maxBullets():int
		{
			return _maxBullets;
		}

		public function get bulletRes():BulletResource
		{
			return _bulletRes;
		}

		public function get pow():int
		{
			return _pow;
		}

		public function get speed():Number
		{
			return _speed;
		}

		public function get pid():int
		{
			return _pid;
		}
		
		public function newBullet():SingleBullet
		{
			return new SingleBullet(_bulletRes,pow);
		}
		
		private function gotValue(obj:Object):void
		{
			_pid = obj.pid;
			_dur = obj.dur;
			_maxDur = obj.maxdur;
			_bulletID = obj.bullet;
			_maxBullets = obj.maxbullets;
			_reload = obj.reload;
			_speed = obj.speed;
			_pow = obj.pow;
			
			_resource = new WeaponResource(1,obj.id,GameMesh.LOD_LOW);
			_resource.addEventListener(Event.COMPLETE,onResourceLoaded);
			_resource.load();
		}
		
		private function onResourceLoaded(e:Event):void
		{
			addChild(_resource);
			Global.phpServer.call("robotServer.getBulletVals", new Responder(gotBulletValue),_bulletID);
		}
		
		private function gotBulletValue(obj:Object):void
		{
			var mid:int = obj.mid;
			var type:int = obj.type;
			var speed:Number = obj.speed;
			var range:Number = obj.range;
			
			_bulletRes = new BulletResource(mid,type,speed,range);
			_bulletRes.addEventListener(Event.COMPLETE,onComplete);
			_bulletRes.load();
		}
		
		private function onComplete(e:Event):void
		{
			var bullet:SingleBullet = new SingleBullet(_bulletRes,pow);
			
			//P2pWorld.bulletAsset[pid] = bullet;
			P2pWorld.mainWeapons[pid] = this;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}